package edu.zut.config;

import org.springframework.core.convert.converter.Converter;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;


/**
 * 基于Converter接口实现自定义类型转换器
 * @Configuration :标明当前类是一个配置类，默认加载到容器中
 */
//@Configuration
public class LocalDateConverter implements Converter<String, LocalDate> {

    /**
     * 实现转换逻辑
     * @param source
     * @return
     */
    @Override
    public LocalDate convert(String source) {
        LocalDate localDate = null;
        if (null == source){
            localDate = LocalDate.now();
        }else {
            localDate = LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        }
        return localDate;
    }
}
